package com.nulldev.util.internal.debugging;

import java.util.List;

import com.nulldev.util.data.Arrays.arrays.FastLists;

public class GDebugger {

	/*
	 * Example Output: / [TOTAL ms] [14 ms] $FUNC1 [11 ms] SUB_FUNC [121 ms] $FUNC2
	 */

	public class Entry {
		private final List<Entry> children;
		private final long start, end;
		private final String functionName;

		public Entry(final long start, final long end, final String functionName) {
			this.children = FastLists.list();
			this.start = start;
			this.end = end;
			this.functionName = functionName;
		}

		public String string() {
			return "[" + (end - start) + " ms] " + this.functionName;
		}

		public boolean hasChildren() {
			return !this.children.isEmpty();
		}

		public List<Entry> getChildren() {
			return this.children;
		}
	}

	private final List<Entry> entries = FastLists.list();
	private final long startTimestamp;
	private volatile long shortStamp;

	public GDebugger() {
		this.startTimestamp = System.currentTimeMillis();
	}

	public void begin() {
		this.shortStamp = System.currentTimeMillis();
	}

	public Entry end(final String functionName) {
		final long e = System.currentTimeMillis();
		final Entry f = new Entry(this.shortStamp, e, functionName);
		this.entries.add(f);
		return f;
	}

	public Entry endNoAppend(final String functionName) {
		final long e = System.currentTimeMillis();
		return new Entry(this.shortStamp, e, functionName);
	}

	public long start() {
		return this.startTimestamp;
	}
}
